package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.InvoiceInfoBean;
import com.koron.css2.baseConfig.bean.ProjectPayeeBean;
import com.koron.css2.baseConfig.mapper.InvoiceInfoMapper;
import com.koron.css2.baseConfig.mapper.ProjectPayeeMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 工程收款主体 列表
 * @author lch
 */
public class ProjectPayeeList implements ServerInterface{

	private static Logger log = LogManager.getLogger(ProjectPayeeList.class);

	@Override 
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info =MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
		try {
			ProjectPayeeBean bean = JsonUtils.objectToPojo(req.getData(), ProjectPayeeBean.class);
			ProjectPayeeMapper mapper = factory.getMapper(ProjectPayeeMapper.class);
			InvoiceInfoMapper invoiceInfoMapper = factory.getMapper(InvoiceInfoMapper.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<ProjectPayeeBean> cdList = mapper.selectList(bean);
			for (ProjectPayeeBean projectPayee : cdList){
				List<InvoiceInfoBean> invoiceInfoList = invoiceInfoMapper.selectByProjectPayeeId(projectPayee.getId());
				projectPayee.setInvoiceInfos(invoiceInfoList);
			}
			info.setData(new PageInfo<>(cdList));
		}catch(Exception e) {
			log.error("工程收款主体列表查询失败",e);
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("工程收款主体列表查询失败");
		}
		return info;
	}
}
